package com.taxshare.operation.service;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.taxshare.operation.view.dto.DocumentDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.minio.dto.MinIOUploadResDTO;
import com.taxshare.minio.utils.MinIOUtils;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.mybatis.utils.DozerBeanUtil;
import com.taxshare.operation.entity.DocumentEntity;
import com.taxshare.operation.mapper.DocumentMapper;
import com.taxshare.operation.view.vo.DocumentVO;
import com.taxshare.response.beans.Response;
import com.taxshare.tools.util.UUIDUitl;
import static com.taxshare.operation.constant.MinIOConstant.BUCKET_NAME;
import static com.taxshare.operation.constant.MinIOConstant.GROUP_DOCUMENT;

/**
 * 文件管理
 *
 * @author liupp
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
public class DocumentService extends ServiceImpl<DocumentMapper, DocumentEntity>{

	@Autowired
    private MinIOUtils  minIOUtils;
	
    /**
     * 获取信息列表,分页
     */
    public Response<IPage<DocumentVO>> queryPageList(PageRequest<DocumentVO> query){
        QueryWrapper<DocumentEntity> queryWrapper = new QueryWrapper<>();
        String documentCode = query.getInfo().getDocumentCode();
        if (StringUtils.isNotEmpty(documentCode)) {
            queryWrapper.like("document_code", documentCode);
        }
        String documentName = query.getInfo().getDocumentName();
        if (StringUtils.isNotEmpty(documentName)) {
            queryWrapper.like("document_name", documentName);
        }
        queryWrapper.orderByDesc("create_time");
        IPage<DocumentVO> result = PageHelp.siamplePage(this, queryWrapper,query.getPage(),DocumentVO.class);
        return Response.success(result);
    }
    
    /**
        * @Title: add
        * @Description: (文件管理新增)
        * @param @param pojo
        * @param @return    参数
        * @return Response    返回类型
        * @throws
     */
    public Response add(DocumentVO pojo){
    	DocumentEntity document =  DozerBeanUtil.map(pojo, DocumentEntity.class);
        QueryWrapper<DocumentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("document_code", document.getDocumentCode());
        List<DocumentEntity> list = this.list(queryWrapper);
        if(list!=null && list.size()!=0){
            return Response.error("T0001","文件编码重复");
        }
    	document.setUuid(UUIDUitl.UUID32(false));
    	document.setCreateTime(new Date());
    	document.setUpdateTime(new Date());
        this.save(document);
        return Response.success();
    }
    
    /**
        * @Title: edit
        * @Description: (修改)
        * @param @param pojo
        * @param @return    参数
        * @return Response    返回类型
        * @throws
     */
    public Response edit(DocumentVO pojo){
    	DocumentEntity document = DozerBeanUtil.map(pojo, DocumentEntity.class);
        QueryWrapper<DocumentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("document_code", document.getDocumentCode());
        queryWrapper.ne("uuid",document.getUuid());
        List<DocumentEntity> list = this.list(queryWrapper);
        if(list!=null && list.size()!=0){
            return Response.error("T0001","文件编码重复");
        }
    	document.setUpdateTime(new Date());
        return Response.success(this.updateById(document));
    }
    
    /**
        * @Title: delete
        * @Description: (删除)
        * @param @param id
        * @param @return    参数
        * @return Response    返回类型
        * @throws
     */
    public Response delete(String uuid){
        return Response.success(this.removeById(uuid));
    }
    
    /**
        * @Title: uploadFile
        * @Description: (文件上传)
        * @param @param file
        * @param @param uuid
        * @param @return    参数
        * @return Response    返回类型
        * @throws
     */
    public Response uploadFile(MultipartFile file,String uuid){
    	String fileName = file.getOriginalFilename();
        MinIOUploadResDTO minIOUploadResDTO = minIOUtils.upload(BUCKET_NAME,GROUP_DOCUMENT ,file,false);
        DocumentEntity document = this.getById(uuid);
        document.setFileName(minIOUploadResDTO.getMinFileName());
        document.setFileUrl(minIOUploadResDTO.getMinFileUrl().split("\\?")[0]);
        document.setUpdateTime(new Date());
        this.updateById(document);
        return Response.success();
    }
    
    /**
        * @Title: down
        * @Description: (文件下载)
        * @param @param response
        * @param @param uuid    参数
        * @return void    返回类型
        * @throws
     */
    public void down(HttpServletResponse response,String uuid){
    	DocumentEntity document = this.getById(uuid);
    	minIOUtils.download(response,BUCKET_NAME,GROUP_DOCUMENT,document.getFileName());
    }

    /**
     * @Description: (模板文件下载)
     * @param response
     * @param documentCode
     */
    public void downTemplate(HttpServletResponse response, String documentCode){
        QueryWrapper<DocumentEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(documentCode)) {
            queryWrapper.eq("document_code", documentCode);
        }
        DocumentEntity document = this.getOne(queryWrapper);
        if(document!=null) {
            minIOUtils.download(response, BUCKET_NAME, GROUP_DOCUMENT, document.getFileName());
        }else{
            log.info("模板下载模板数据不存在");
        }
    }
}
